/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.enterprise.web;

import org.crystalrange.BaseController;
import org.crystalrange.Message;
import org.crystalrange.enterprise.model.StorageBin;
import org.crystalrange.enterprise.model.StorageType;
import org.crystalrange.enterprise.model.StorageUnit;
import org.crystalrange.enterprise.model.StorageUnitType;
import org.crystalrange.enterprise.model.WarehouseNumber;
import org.crystalrange.enterprise.service.StorageUnitService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.Window;

public class StorageUnitController extends BaseController {

    protected StorageUnitService storageUnitService;
    protected StorageUnit storageUnit;

    public StorageUnitService getStorageUnitService() {
        return this.storageUnitService;
    }

    public void setStorageUnitService(StorageUnitService storageUnitService) {
        this.storageUnitService = storageUnitService;
    }

    public StorageUnit getStorageUnit() {
        return this.storageUnit;
    }

    public void setStorageUnit(StorageUnit storageUnit) {
        this.storageUnit = storageUnit;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        if (this.execution.getParameter("Id") != null) {
            this.storageUnit = this.storageUnitService.find(
                    new Long(this.execution.getParameter("Id")));
            this.labelTitle.setValue("Edit Storage Unit");
        } else {
            this.storageUnit = this.storageUnitService.create(
                    SecurityContextHolder.getContext().getAuthentication().getName());
            this.labelTitle.setValue("Create Storage Unit");
        }

        component.setAttribute("controller", this);
    }

    public void onClick$menuitemStorageUnitSave(Event event) throws Exception {
        this.storageUnitSave();
    }

    public void onClick$toolbarbuttonStorageUnitSave(Event event) throws Exception {
        this.storageUnitSave();
    }

    public void onOpen$bandboxWarehouseNumber(Event event) throws Exception {
        Window window = (Window) this.execution.createComponents("~./zul/html/warehouseNumberSelect.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            WarehouseNumber selectedWarehouseNumber = (WarehouseNumber) this.execution.getAttribute("selectedWarehouseNumber");
            this.storageUnit.setWarehouseNumber(selectedWarehouseNumber);
        }

        this.binder.loadAll();
    }

    public void onOpen$bandboxStorageType(Event event) throws Exception {
        Window window = (Window) this.execution.createComponents("~./zul/html/storageTypeSelect.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            StorageType selectedStorageType = (StorageType) this.execution.getAttribute("selectedStorageType");
            this.storageUnit.setStorageType(selectedStorageType);
        }

        this.binder.loadAll();
    }

    public void onOpen$bandboxStorageBin(Event event) throws Exception {
        Window window = (Window) this.execution.createComponents("~./zul/html/storageBinSelect.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            StorageBin selectedStorageBin = (StorageBin) this.execution.getAttribute("selectedStorageBin");
            this.storageUnit.setStorageBin(selectedStorageBin);
        }

        this.binder.loadAll();
    }

    public void onOpen$bandboxStorageUnitType(Event event) throws Exception {
        Window window = (Window) this.execution.createComponents("~./zul/html/storageUnitTypeSelect.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            StorageUnitType selectedStorageUnitType = (StorageUnitType) this.execution.getAttribute("selectedStorageUnitType");
            this.storageUnit.setStorageUnitType(selectedStorageUnitType);
        }

        this.binder.loadAll();
    }

    private void storageUnitSave() throws Exception {
        this.storageUnitService.save(this.storageUnit,
                SecurityContextHolder.getContext().getAuthentication().getName());
        this.sessionScope.put("CRYSTALRANGE_MESSAGE", new Message("Saved successfully!"));
        Clients.evalJavaScript("window.history.back();");
    }
}
